package svgedit.gui.manipulators;

import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.event.UndoableEditEvent;

import svgedit.gui.View;
import svgedit.svg.SVGCircleElement;
import svgedit.svg.SVGDocument;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGLength;
import svgedit.svg.UndoPaint;

/** Manipulator for drawing a circle.  The user clicks and drags within the view
 *  to define a circle's center point and radius.
 */
public class InsertCircleManipulator extends Manipulator {

	private SVGCircleElement circle;

	/** Creates a manipulator for drawing a circle within the given view.
	 *
	 * @param view the view this manipulator belongs to
	 */
	public InsertCircleManipulator(View view) {
		super(view);
	}

	@Override
	public boolean mousePressed(MouseEvent e) {
		circle = new SVGCircleElement(getView().getDocument());
		circle.getCX().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, e.getX());
		circle.getCY().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, e.getY());
		circle.getR().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 0.0f);

		circle.setStyleFromStylable(getView().getDefaultStyle());
		return true;
	}

	@Override
	public boolean mouseDragged(MouseEvent e) {
		circle.getR().setValue(e.getX() - circle.getCX().getValue());
		return true;
	}

	@Override
	public boolean mouseReleased(MouseEvent e) {
		View view = getView();
		SVGDocument document = view.getDocument();
		document.getRootGroup().appendChild(circle);
		undo();
		document.setModified(true);
		view.setSelectedElement(circle);
		view.setSelectionManipulator();
		circle = null;
		return true;
	}

	@Override
	public void paint(Graphics2D g) {
		if (circle != null)
			getView().paintElement(g, circle);
	}

	@Override
	public Cursor getCursor(Point point) {
		return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
	}

	@Override
	public SVGElement getElement()
	{
		return (SVGElement)circle;

	}

	public void undo()
	{ 
		View view = getView();
		SVGElement se = null;
		ArrayList<SVGElement> al = new ArrayList<SVGElement>();
		ArrayList<SVGElement> a2 = new ArrayList<SVGElement>();
		al=getView().getDocument().getRootGroup().getChildren2();
		//Collections.reverse(al);
		for (SVGElement elem : getView().getDocument()) {
			a2.add(elem);                                 
		}
		for(int i=a2.size()-1;i>a2.size()-2;i--)
		{
			se = a2.get(i);
		}
		view.undoManager.undoableEditHappened(new UndoableEditEvent(
				getView(), new UndoPaint(se,al)));
	}

}
